package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐，同时添加对应的菜品
     * @param setmealDto
     */
    @Override
    public void addSetmealAndDish(SetmealDto setmealDto) {
        //添加套餐数据
        this.save(setmealDto);
        //添加套餐对应的菜品数据
        //setmealDishes中的setmealId没有值需要通过stream流写入
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 修改套餐时的数据回显
     * @param id
     * @return
     */
    @Override
    public SetmealDto setmealDtoInfo(Long id) {
        //创建一个setmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        //根据id查询setmeal
        Setmeal setmeal = this.getById(id);
        //将setmeal赋值给setmealDto
        BeanUtils.copyProperties(setmeal,setmealDto);
        //根据id查询对应的菜品
        //构建查询函数
        LambdaQueryWrapper<SetmealDish> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,SetmealDish::getSetmealId,id);
        queryWrapper.orderByDesc(SetmealDish::getUpdateTime);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        //将list赋值给setmealDto
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     *修改套餐和对应的菜品信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updatesetmealWithDish(SetmealDto setmealDto) {
        //修改套餐
        this.updateById(setmealDto);
        //修改套餐下对应的菜品
        //首先，查询套餐下对应的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //根据套餐id查询菜品
        queryWrapper.eq(setmealDto.getId()!=null, SetmealDish::getDishId,setmealDto.getId());
        //清空套餐对应的菜品
        setmealDishService.remove(queryWrapper);
        //将setmealDto下的菜品信息写入到SetmealDish表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //此时的setmealDishes中没有套餐id
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteSetmeal(List<Long> ids) {
        //根据id查询套餐状态
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        lambdaQueryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(lambdaQueryWrapper);
        if (count > 0 ){
            throw  new CustomException("套餐正在售卖中，不能删除");
        }

        //首先删除setmealDish中的数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper);

        //再删除Setmeal中的数据
        this.removeByIds(ids);

    }

    /**
     * 批量修改套餐起售状态
     * @param status
     * @param ids
     */
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //根据ids查询套餐
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> list = this.list(queryWrapper);
        List<Setmeal> setmealList = list.stream().map((item)->{
            //将status赋值给item
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());
        //修改套餐状态
        this.updateBatchById(setmealList);

    }


}
